package com.cobo.common.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class CollecUtils {

    /*
    * 查找到匹配list的下一个
    * */
    public static <T> Optional<T> findNext(ArrayList<T> cols, Predicate<T> predicate) {
        if (cols == null) {
            return Optional.empty();
        }

        boolean match = false;
        for (T col : cols) {
            if (predicate.test(col)) {
                match = true;
            }
            if (match) {
                return Optional.of(col);
            }
        }
        return Optional.empty();
    }


    public static <T> Optional<T> findFirst(Collection<T> cols, Predicate<T> predicate) {
        if (cols == null) {
            return Optional.empty();
        }
        return cols.stream().filter(predicate).findFirst();
    }

    public static <T> Optional<List<T>> list(Collection<T> cols, Predicate<T> predicate) {
        if (cols == null) {
            return Optional.empty();
        }
        return Optional.of(
                        cols.stream().filter(predicate).collect(Collectors.toList())
                );
    }
}
